package com.foo.CR;

import android.app.ListActivity;
import android.content.Intent;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tejas
 * Date: 12/30/10
 * Time: 11:42 AM
 *
 * Queries the WiFiManger to get configured (remembered) wifi networks
 * Displays to the user as a list.
 * On selection of a WiFi SSID, the user is redirected to the SelectProfile Activity.
 * The selected WiFi SSID is passed as an Intent Extra
 */
public class SelectWiFi extends ListActivity {

    private static String[] items;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.select_main);

        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        List<WifiConfiguration> wifiConfigurations = wifiManager.getConfiguredNetworks();

        if (wifiConfigurations.size() > 0)
        {
            items = new String[wifiConfigurations.size()];
            MyWiFiMap myWiFiMap = MyWiFiMap.getMyWifiMap(getApplicationContext());

            for (int i = 0; i < wifiConfigurations.size(); i++)
            {
                String tmp = wifiConfigurations.get(i).SSID;
                tmp = Utils.cleanWifiID(tmp);
                switch (myWiFiMap.getActionforSSID(tmp))
                {
                   case Constants.RINGER_ACTION_LOUD:
                       tmp = tmp + "[LOUD]";
                       break;
                    case Constants.RINGER_ACTION_VIBRATE:
                        tmp = tmp + "[VIBRATE]";
                        break;
                    case Constants.RINGER_ACTION_SILENT:
                        tmp = tmp + "[SILENT]";
                        break;
                }

                items[i] = tmp;
            }

            setListAdapter(new ArrayAdapter<String>(this,
                    R.layout.wifi_row, R.id.wifiProfName, items));
        }

    }

    public void onListItemClick(ListView parent, View v,
                                int position, long id) {
        Intent assignProfile = new Intent();
        assignProfile.setClassName("com.foo.CR", "com.foo.CR.SelectProfile");
        String wifiID = items[position];
        assignProfile.putExtra("SSID", Utils.removeProfFromWifiID(wifiID));
        startActivity(assignProfile);
        finish();
    }

}
